//
//  main.m
//  OC05-HomeWork!(周六)
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 高雅馨. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "ProModel.h"
#import "CityModel.h"
#import "Area Model.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
    /**
     *  一、
     
     1.创建1个包含5个字符串元素的数组；
     2.用for循环倒序输出所有元素；
     3.用for...in完成所有字符串元素的拼接(生成一个
     新的字符串)，并输出拼接好的字符串。
     二、
     
     1.创建1个包含5个person对象的数组(姓名用英文)；
     2.用for循环找到年龄最大的Person对象；
     3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
     三、
     
     1.使用第二题目中生成的数组；
     2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
     3.使用NSSortDescriptor，按姓名进行排序。
     四、
     
     处理省市区文件中的area.txt
     将省市区进行分割处理, 省里包含市, 市里包含区
     1. 先用数组字典表示
     2. 将数组字典的处理方式转换成数组模型(Class)
     五、拓展
     
     上述作业做完, 处理Location1.txt
     要求同第四题。自己寻找规律
     */
    // 1⃣️
    NSArray *stringArr = @[@"🇨🇳台湾",@"🇨🇳日本", @"🇨🇳钓鱼岛", @"🇨🇳香港", @"🇨🇳澳门"];
    NSMutableArray *newStringArr = [NSMutableArray array];
    for (NSInteger i = stringArr.count - 1; i >= 0; i--) {
        [newStringArr addObject:stringArr[i]];
    }
    NSLog(@"%@", newStringArr);
    NSString *newString = [[NSString alloc] init];
    for (NSString *str in stringArr) {
        newString =  [newString stringByAppendingString:str];
    }
    NSLog(@"%@", newString);
    // 2⃣️
    Person *person1 = [Person nurseWithName:@"Aaron" Age:18 Sex:@"girl"];
    Person *person2 = [Person nurseWithName:@"Boris" Age:17 Sex:@"boy"];
    Person *person3 = [Person nurseWithName:@"Colin" Age:20 Sex:@"man"];
    Person *person4 = [Person nurseWithName:@"Darcy" Age:19 Sex:@"man"];
    Person *person5 = [Person nurseWithName:@"Edison" Age:21 Sex:@"woman"];
    NSArray *personArr = @[person1, person2, person3, person4, person5];
    int maxAge = 0;
    for (int i = 0; i < personArr.count; i++) {
        Person *temp = personArr[i];
        if (maxAge < temp.age) {
            maxAge = temp.age;
            [personArr indexOfObject:personArr[i]];
        }
    }
    NSLog(@"%d", maxAge);
    for (Person *per in personArr) {
        if (per.age > 18) {
            per.name = [per.name stringByAppendingString:@"-brother"];
        }
        NSLog(@"%@", per.name);
    }
    // 3⃣️
    NSMutableArray *personMutArr = [personArr mutableCopy];
    [personMutArr sortUsingSelector:@selector(sortByAge:)  ];
    for (Person *temp in personMutArr) {
        NSLog(@"名字: %@ -- 年龄:%d -- 性别:%@", temp.name, temp.age, temp.sex);
    }
    NSSortDescriptor *sortDescriptor =
    [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    [personMutArr sortUsingDescriptors:@[sortDescriptor]];
    NSLog(@"%@", personMutArr);
    // 4⃣️
#if 1
    {
        //获取文本路径
        NSString *path = @"/Users/dllo/DLS160710/Objective-C/HomeWork/OC05-HomeWork!(周六)/OC05-HomeWork!(周六)/Homework-05/area.txt";
        //读取文本内容，转化成字符串
        NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        //NSLog(@"%@", str);
        //以换行进行字符串分割，获得每行数据
        NSArray * strArray = [str componentsSeparatedByString:@"\n"];
        //定义一个国家数组
        NSMutableArray * countryArray = [NSMutableArray array];
        //遍历数组内每一行信息
        for (NSString * temp in strArray) {
            //NSLog(@"%@", temp);
            //字符串不已空格开头的为省
            if(![temp hasPrefix:@" "]) {
                //找到一个省名，为省创建一个字典
                NSMutableDictionary *proDictionary = [NSMutableDictionary dictionary];
                //存入省名
                [proDictionary setObject:temp forKey:@"proName"];
                //创建当前声的所辖市数组
                NSMutableArray *cityArray = [NSMutableArray array];
                //将市的数组存入相应的省字典中
                [proDictionary setObject:cityArray forKey:@"cities"];
                //将当前省加入国家数组内
                [countryArray addObject:proDictionary];
            } else if ([temp hasPrefix:@" "@" "] && ![temp hasPrefix:@"   "]) {
                //判断当前行是以两个空格开头，并且不以四个空格开头，那么就为市
                //为当前的市创建一个字典
                NSMutableDictionary *cityDictionary = [NSMutableDictionary dictionary];
                //将当前行（市名）存入当前市字典中
                [cityDictionary setObject:temp forKey:@"cityName"];
                //为当前市所辖区创建数组
                NSMutableArray *areaArray = [NSMutableArray array];
                [cityDictionary setObject:areaArray forKey:@"areas"];
                //找到当前市所对应的省字典（即为数组内最后一个元素）
                NSMutableDictionary *proDic = [countryArray lastObject];
                //找到当前省下的数组 将当前市加入到对应的省下的市数组内
                [[proDic objectForKey:@"cities"] addObject:cityDictionary];
            } else {
                //为区创建字典
                NSMutableDictionary *areaDic = [NSMutableDictionary dictionary];
                //设置区的名字
                [areaDic setObject:temp forKey:@"areaName"];
                //先找到省字典 省字典下的市数组 市数组下的当前市 当前市数组下的区数组 当前区加到当前数组中
                [[[[[countryArray lastObject] objectForKey:@"cities"] lastObject] objectForKey:@"areas"] addObject:areaDic];
            }
        }
         NSLog(@"%@", countryArray);
        NSMutableArray *allProArray = [NSMutableArray array];
        for (NSString *area in strArray) {
            if (![area hasPrefix:@""]) {
                ProModel *pro = [ProModel area_ModelWithName:area];
                [allProArray addObject:pro];
            } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"   "]) {
                ProModel *pro = [allProArray lastObject];
                CityModel *city = [CityModel area_ModelWithName:area];
                [pro.array addObject:city];
            } else {
                CityModel *city = [[[allProArray lastObject] array] lastObject];
                Area_Model *area1 = [Area_Model area_ModelWithName:area];
                [city.array addObject:area1];
            }
        }
        NSLog(@"%@", allProArray);
    }
#endif
    // 5⃣️
#if 1
    {
        NSString *filePath = @"/Users/dllo/DLS160710/Objective-C/HomeWork/OC05-HomeWork!(周六)/OC05-HomeWork!(周六)/Homework-05/Location1.txt";
        NSString *buffer = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        // 根据,分割字符串,得到所有城市的数组
        NSArray *strArray = [buffer componentsSeparatedByString:@","];
        NSMutableArray * countryArray = [NSMutableArray array];
        // 根据每个城市中的|处理字符串, 重新生成字典的数组
        for (NSString * temp in strArray) {
            // 根据|切割字符串
            NSArray *arrayCity = [temp componentsSeparatedByString:@"|"];
            // 根据切割生成的数组, 生成新的城市字典
            NSMutableDictionary *provinceDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[arrayCity firstObject],@"cityName", [arrayCity lastObject], @"cityNumber", nil];
            // 添加到新的数组
            [countryArray addObject:provinceDic];
        }
        // 排序(重要)
        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
        [countryArray sortUsingDescriptors:@[sort]];
     //   NSLog(@"%@", countryArray);
        NSMutableArray * finalArray = [NSMutableArray array];
        for (NSMutableDictionary *cityDic in countryArray) {
            NSString *cityNumber = cityDic[@"cityNumber"];
            if ([cityNumber hasSuffix:@"0000"]) {
                NSMutableDictionary *provinceDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
                [provinceDic setObject:[NSMutableArray array] forKey:@"cityArray"];
                [finalArray addObject:provinceDic];
            } else if ([cityNumber hasSuffix:@"00"] && ![cityNumber hasSuffix:@"0000"]) {
                NSMutableDictionary *provinceDic = [finalArray lastObject];
                NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
                NSMutableDictionary *dicCity = [NSMutableDictionary dictionaryWithDictionary:cityDic];
                [dicCity setObject:[NSMutableArray array] forKey:@"areaArray"];
                [cityArray addObject:dicCity];
            } else {
               NSMutableArray *areaArray = [[[[finalArray lastObject]objectForKey:@"cityArray"] lastObject] objectForKey:@"areaArray"];
                NSMutableDictionary *areaDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
                [areaArray addObject:areaDic];
            }
            }
        NSLog(@"%@", finalArray);
        NSMutableArray *allTempCityArr = [NSMutableArray array];
        for (NSString *cityString in strArray) {
            NSArray *city = [cityString componentsSeparatedByString:@"|"];
            TempCity *tempCity = [[TempCity alloc] initWithName:[city firstObject] number:[city lastObject]];
            [allTempCityArr addObject:tempCity];
        }
        NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
        [allTempCityArr sortUsingDescriptors:@[sortTempCity]];
        NSMutableArray *finalClassArray = [NSMutableArray array];
        for (TempCity *tempCity in allTempCityArr) {
            if ([tempCity.cityNumber hasSuffix:@"0000"]) {
                ProModel *pro = [ProModel area_ModelWithTempCity:tempCity];
                [finalClassArray addObject:pro];
            } else if ([tempCity.cityNumber hasSuffix:@"00"] && ![tempCity.cityNumber hasSuffix:@"0000"]) {
                ProModel *pro = [finalClassArray lastObject];
                CityModel *city = [CityModel area_ModelWithTempCity:tempCity];
                [pro.array addObject:city];
            } else {
                ProModel *pro = [finalClassArray lastObject];
                CityModel *city = [pro.array lastObject];
                Area_Model *area = [Area_Model area_ModelWithTempCity:tempCity];
                [city.array addObject:area];
            }
        }
        NSLog(@"%@", finalClassArray);

    }
#endif
    return 0;
}
